﻿using System.IO;
using System.Net;
using System.Text;

namespace FluentWebRequest
{
    internal class RequestMethodFactory : IRequestMethodFactory
    {
        private HttpWebRequest _webRequest;

        public string GetDataFromUrl(string url, string parametters, RequestMethodType type)
        {
            _webRequest = type == RequestMethodType.Get
                              ? new GetRequestMethodBuilder(_webRequest, url, parametters).GetDataFromUri()
                              : new PostRequestMethodBuilder(_webRequest, url, parametters).GetDataFromUri();

            var result = string.Empty;
            using (var response = (HttpWebResponse)_webRequest.GetResponse())
            {
                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                        using (var readStream = new StreamReader(responseStream, Encoding.UTF8))
                        {
                            result = readStream.ReadToEnd();
                        }
                }
            }

            return result;
        }
    }
}